package com.school.base.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.school.base.dao.ILessonDao;
import com.school.base.model.Lesson;
import com.school.common.tags.PageBean;

/**
 * @author gaobiao
 * 
 */
public class LessonService {
	private static Logger logger = Logger.getLogger(LessonService.class);
	private ILessonDao lessonDao;

	public ILessonDao getLessonDao() {
		return lessonDao;
	}

	public void setLessonDao(ILessonDao lessonDao) {
		this.lessonDao = lessonDao;
	}

	public Lesson getLessonById(int id) {
		return lessonDao.getLessonById(id);
	}

	public List<Lesson> getLessonList(Lesson lesson, PageBean pageBean) {
		if (pageBean == null) {
			pageBean = new PageBean();
		}
		pageBean.setTotalRecordCount(lessonDao.getTotalCount(lesson));
		return lessonDao.getLessonList(lesson, pageBean.getOffset(), pageBean.getPageSize());
	}

	public void updateLesson(Lesson lesson) throws Exception {
		if (lesson != null) {
			if (lesson.getName() == null || lesson.getName().isEmpty()) {
				logger.error("Upadte lesson fail:lesson's name is empty.");
				throw new Exception("课时名称不能为空");
			}
			if (lesson.getCourseId() == 0) {
				logger.error("Upadte lesson fail:lesson's Course is empty.");
				throw new Exception("所属课程名称不能为空");
			}
		} else {
			throw new Exception("Lesson对象不能为空");
		}
		lessonDao.updateLesson(lesson);
	}

	public void deleteLesson(int id) {
		lessonDao.deleteLesson(id);
	}

	public void insertLesson(Lesson lesson) throws Exception {
		if (lesson != null) {
			if (lesson.getName() == null || lesson.getName().isEmpty()) {
				logger.error("Insert lesson fail:lesson's name is empty.");
				throw new Exception("课时名称不能为空");
			}
			if (lesson.getCourseId() == 0) {
				logger.error("Insert lesson fail:lesson's Course is empty.");
				throw new Exception("所属课程名称不能为空");
			}
		} else {
			throw new Exception("Lesson对象不能为空");
		}
		lessonDao.insertLesson(lesson);
	}
}
